package servlet;

import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.Customer;
import pojo.Order;
import dao.ShopOrderDao;
import daoImpl.DaoImplFactory;

/**
 * Servlet implementation class AddServlet
 */
public class AddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    private ShopOrderDao shopOrderDao = DaoImplFactory.getShopOrderDao();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Customer cust = (Customer) request.getSession().getAttribute("cust");
		Map<String, Integer> map = (Map<String, Integer>) request.getSession().getAttribute("cart");
		String uId = cust.getuId();
		for ( String title: map.keySet()) {
			Order order = new Order(UUID.randomUUID().toString(), uId, title, map.get(title), new Date());
			shopOrderDao.addOrder(order);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
